/**
 * @作者 chengxinxiaofeng
 * @时间 2019年7月15日10:30:20
 */
package com.chinaunicom.tour.sys.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.chinaunicom.tour.sys.constant.CommonConstants;
import com.chinaunicom.tour.sys.constant.ResultConstants;
import com.chinaunicom.tour.sys.entity.SysDepartment;
import com.chinaunicom.tour.sys.model.SysDepartmentModel;
import com.chinaunicom.tour.sys.service.SysDepartmentService;
import com.chinaunicom.tour.sys.util.ObjectRestResponse;
import com.chinaunicom.tour.sys.util.TableResultResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "部门管理", tags = { "部门管理" })
@RestController
@RequestMapping("department")
public class SysDepartmentController {

    @Autowired
    SysDepartmentService sysDepartmentService;
    
    @ApiOperation(value = "新增", notes = "")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<SysDepartment> add(@RequestBody @Validated SysDepartment entity,BindingResult results) {
        ObjectRestResponse<SysDepartment> orr = new ObjectRestResponse<SysDepartment>();
        orr.setData(null);
        try {
        	
        	//不为空校验为空
            if (results.hasErrors()) {
                orr.setStatus(ResultConstants.ERROR_CODE);
                orr.setMessage(results.getFieldError().getDefaultMessage());
                return orr;
            }

             SysDepartmentModel queryModel = new SysDepartmentModel();
	         queryModel.setName(entity.getName());
	         List<SysDepartment> departmentlist = sysDepartmentService.findList(queryModel);
	         if(departmentlist != null && departmentlist.size() > 0){
	         	 orr.setStatus(ResultConstants.ERROR_CODE);
	             orr.setMessage("部门名称已存在");
	             return orr;
	         }
	        //设置数据来源于创建
	        entity.setDataFrom(CommonConstants.FROM_CREATE);
            sysDepartmentService.add(entity);
            orr.setStatus(ResultConstants.SUCCESS_CODE);
            orr.setMessage("添加成功");
            return orr;
        } catch (Exception e) {
            e.printStackTrace();
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("添加失败" + e.getMessage());
            return orr;
        }
    }

    @ApiOperation(value = "修改", notes = "")
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse<SysDepartment> update(@RequestBody @Validated SysDepartment entity,BindingResult results) {
        ObjectRestResponse<SysDepartment> orr = new ObjectRestResponse<SysDepartment>();
        orr.setData(null);
        try {
        	if (results.hasErrors()) {
                 orr.setStatus(ResultConstants.ERROR_CODE);
                 orr.setMessage(results.getFieldError().getDefaultMessage());
                 return orr;
            }

        	SysDepartmentModel queryModel = new SysDepartmentModel();
            queryModel.setName(entity.getName());
            List<SysDepartment> rolelist = sysDepartmentService.findList(queryModel);
            //是否已存在非当前更新记录的部门名
            if(rolelist != null && rolelist.size() > 0 &&
            	StringUtils.isNotEmpty(entity.getId()) &&
            	StringUtils.isNotEmpty(rolelist.get(0).getId()) &&
            	!(entity.getId().equals(rolelist.get(0).getId()))){
            	orr.setStatus(ResultConstants.ERROR_CODE);
                orr.setMessage("部门名称已存在");
                return orr;
            }
	        //设置数据来源于创建
	        entity.setDataFrom(CommonConstants.FROM_CREATE);
            sysDepartmentService.update(entity);
            orr.setStatus(ResultConstants.SUCCESS_CODE);
            orr.setMessage("修改成功");
            return orr;
        } catch (Exception e) {
            e.printStackTrace();
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("修改失败" + e.getMessage());
            return orr;
        }
    }

    @ApiOperation(value = "删除", notes = "")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse<SysDepartment> delete(@PathVariable String id) {
        ObjectRestResponse<SysDepartment> orr = new ObjectRestResponse<SysDepartment>();
        orr.setData(null);

        try {
          	SysDepartmentModel queryModel = new SysDepartmentModel();
            queryModel.setParentid(id);
            //执行删除
            sysDepartmentService.delete(id);
            orr.setStatus(ResultConstants.SUCCESS_CODE);
            orr.setMessage("删除成功");
            return orr;
        } catch (Exception e) {
            e.printStackTrace();
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("删除失败" + e.getMessage());
            return orr;
        }
    }

    @ApiOperation(value = "分页查询", notes = "")
    @RequestMapping(value = "/pageQuery", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<SysDepartment> pageQuery(@ModelAttribute SysDepartmentModel queryModel) {
        TableResultResponse<SysDepartment> trr = new TableResultResponse<SysDepartment>();

        try {
            List<SysDepartment> list = sysDepartmentService.pageQuery(queryModel);
            trr.getData().setTotal(sysDepartmentService.pageQueryCount(queryModel));
            trr.getData().setRows(list);
            trr.setStatus(ResultConstants.SUCCESS_CODE);
            trr.setMessage("查询成功");
            return trr;
        } catch (Exception e) {
            e.printStackTrace();
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("查询失败" + e.getMessage());
            trr.getData().setTotal(0);
            trr.getData().setRows(null);
            return trr;
        }
    }

    @ApiOperation(value = "查询所有", notes = "")
    @RequestMapping(value = "/findList", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<SysDepartment> findList(@ModelAttribute SysDepartmentModel queryModel) {
        TableResultResponse<SysDepartment> trr = new TableResultResponse<SysDepartment>();

        try {
            List<SysDepartment> list = sysDepartmentService.findList(queryModel);
            trr.getData().setTotal(list.size());
            trr.getData().setRows(list);
            trr.setStatus(ResultConstants.SUCCESS_CODE);
            trr.setMessage("查询成功");
            return trr;
        } catch (Exception e) {
            e.printStackTrace();
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("查询失败" + e.getMessage());
            trr.getData().setTotal(0);
            trr.getData().setRows(null);
            return trr;
        }
    }

    @ApiOperation(value = "根据id查询", notes = "")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<SysDepartment> findById(@PathVariable String id) {
        ObjectRestResponse<SysDepartment> orr = new ObjectRestResponse<SysDepartment>();

        try {
        	SysDepartment sysDepartment = sysDepartmentService.findById(id);
            orr.setData(sysDepartment);
            orr.setStatus(ResultConstants.SUCCESS_CODE);
            orr.setMessage("查询成功");
            return orr;
        } catch (Exception e) {
            e.printStackTrace();
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("查询失败" + e.getMessage());
            return orr;
        }
    }
}
